package com.lizheng.yunservice.goods.service.impl;


import com.lizheng.yunservice.common.domain.ApiRestResponse;
import com.lizheng.yunservice.goods.mapper.VtpCustomerLikesMapper;
import com.lizheng.yunservice.goods.model.VtpCustomerLikes;
import com.lizheng.yunservice.goods.model.VtpCustomerLikesDTO;
import com.lizheng.yunservice.goods.service.VtpCustomerLikesService;
import org.springframework.stereotype.Service;

@Service
public class VtpCustomerLikesServiceImpl implements VtpCustomerLikesService {

    private final VtpCustomerLikesMapper likesMapper;

    public VtpCustomerLikesServiceImpl(VtpCustomerLikesMapper likesMapper) {
        this.likesMapper = likesMapper;
    }

    @Override
    public ApiRestResponse<String> saveWxLikes(VtpCustomerLikesDTO dto, Long userId, String username) {
        // 1. 参数校验
        ApiRestResponse<String> validationResult = validateLikesRequest(dto);
        if (validationResult != null) {
            return validationResult;
        }
        // 2. 类型转换
        String likesType = convertType(dto.getObjectType());
        if (!"goods".equals(likesType)) {
            return ApiRestResponse.fail(600, "不支持的对象类型");
        }
        // 3. 构建实体
        VtpCustomerLikes likes = buildLikesEntity(dto, userId, username, likesType);
        // 4. 检查是否已点赞
        if (likesMapper.existsLike(likes) > 0) {
            return ApiRestResponse.fail("您已经点过赞了");
        }
        else {
            if(likesMapper.existsLikeRecord(likes) > 0) {
                return likesMapper.updateLike(likes)>0
                        ? ApiRestResponse.ok("点赞成功")
                        : ApiRestResponse.fail("点赞失败");
            }
            else {
                return likesMapper.insert(likes) > 0
                        ? ApiRestResponse.ok("点赞成功")
                        : ApiRestResponse.fail("点赞失败");
            }
        }
    }

    @Override
    public ApiRestResponse<String> cancelWxLikes(VtpCustomerLikesDTO dto, Long userId, String username) {
        // 1. 参数校验
        if (dto.getObjectId() == null) {
            return ApiRestResponse.fail(600, "参数不完整");
        }
        // 3. 构建实体
        VtpCustomerLikes likes = buildLikesEntity(dto, userId, username,"goods");
        // 4. 检查是否已点赞
        if (likesMapper.existsLike(likes) == 0) {
            return ApiRestResponse.fail("您还未点赞，无法取消");
        }
        // 5. 执行取消点赞操作
        return likesMapper.cancelLike(likes) > 0
                ? ApiRestResponse.ok("取消点赞成功")
                : ApiRestResponse.fail("取消点赞失败");
    }

    /**
     * 验证点赞请求参数
     */
    private ApiRestResponse<String> validateLikesRequest(VtpCustomerLikesDTO dto) {
        if (dto.getObjectId() == null || dto.getObjectType() == null) {
            return ApiRestResponse.fail(600, "参数不完整");
        }
        return null;
    }

    /**
     * 构建点赞实体
     */
    private VtpCustomerLikes buildLikesEntity(VtpCustomerLikesDTO dto, Long userId,
                                              String username, String likesType) {
        VtpCustomerLikes likes = new VtpCustomerLikes();
        likes.setCustomerId(userId);
        likes.setUserName(username);
        likes.setObjectId(dto.getObjectId());
        likes.setLikesType(likesType);
        return likes;
    }

    private String convertType(Integer objectType) {
        return objectType == 2 ? "goods" : null;
    }
}